package uf.audit.demo.controller;

import java.util.List;

import com.jfinal.plugin.activerecord.Record;

import uf.audit.demo.util.JSONUtil;
import uf.audit.tax.platform.core.annotation.Controller;
import uf.audit.tax.platform.core.db.DBProFacotry;

@Controller(url = "/wgc", view = "/pages/wgc")
public class GwcController extends BaseController {

	public void cart(){
		render("cart.html");
	}
	public void rent() {
		render("rent.html");
	}
	
	/**
	 * 购物车展示
	 */
	public void findAllGwcByUsername() {
		
		Object obj = this.getSession().getAttribute("user");
		Record rec1 = JSONUtil.parseRecord(JSONUtil.toJson(obj));
		String username = rec1.get("username");
		String findAllGwcByUsernameSql = "select * from gwc where username = ?";
		List<Record> list = DBProFacotry.getPro().find(findAllGwcByUsernameSql, username);
		if(list.size()==0){
			renderJson("num","购物车为零");
		}else{
			renderJson(list);
		}
	}
	
	/**
	 * 增删购物车车的数量
	 */
	
	public void subNum() {
		Object obj = this.getSession().getAttribute("user");
		Record rec1 = JSONUtil.parseRecord(JSONUtil.toJson(obj));
		String username = rec1.get("username");
		Record rec = JSONUtil.parseRecord(getPostData());
		String xm = rec.getStr("xm");
		String subNum = "update gwc set num = num-1 where username = ? and xm = ? ";
		DBProFacotry.getPro().update(subNum,username,xm);
		renderSuccess();
	}
	
	public void addNum() {
		Object obj = this.getSession().getAttribute("user");
		Record rec1 = JSONUtil.parseRecord(JSONUtil.toJson(obj));
		String username = rec1.get("username");
		Record rec = JSONUtil.parseRecord(getPostData());
		String xm = rec.getStr("xm");
		String subNum = "update gwc set num = num+1 where username = ? and xm = ? ";
		DBProFacotry.getPro().update(subNum,username,xm);
		renderSuccess();
	}
	
	/**
	 * 删除购物车的车辆
	 */
	public void deleteNum() {
		Object obj = this.getSession().getAttribute("user");
		Record rec1 = JSONUtil.parseRecord(JSONUtil.toJson(obj));
		String username = rec1.get("username");
		Record rec = JSONUtil.parseRecord(getPostData());
		String xm = rec.getStr("xm");
		String deleteNum = "delete from gwc where username = ? and xm = ? ";
		DBProFacotry.getPro().update(deleteNum,username,xm);
		renderSuccess();
	}
	
	
}
